To set up mock server, you need only three things:
Follow the steps below to set-up mock server:
mock-server
.package.json
under mock-server
directory.Add Mock Server module dependency in package.json
as shown below.
{
"name": "test-midway",
"dependencies": {
"testarmada-midway": "^1.0.1"
},
"scripts": {
"start-mock": "node mocks/run-mock-server-console.js"
}
}
mocks
under the mock-server
directory.Under the directory mocks
, create a file endpoints.js
with the following code - This file will contain the routes that you want to mock
var midway = require('testarmada-midway'); // Required
midway.id('example'); // Required
// add a route that returns a message "hello world"
midway.route({
id: 'message',
label: 'hello message',
path: '/message',
method: 'GET',
variantLabel: 'hello world',
handler: function(req, reply) {
reply({message: 'hello world'});
}
});
Under the directory mocks
, create a file run-mock-server-console.js
with the following code - This file will contain the start-up script for mock server
// load mocked endpoint
require('./endpoints');
var midway = require('testarmada-midway');
midway.start({
host: 'localhost',
mockedDirectory: './mocks', // this can be provided as an absolute path as well.
port: 8080,
project: 'HelloMidway', //Replace HelloMidway with your project name (without dashes).
});
Now open terminal/cmd prompt and navigate to the directory mock-server
and run the following command to install Mock Server and dependencies:
npm install
To start mock-server use the following command and than go to http://localhost:8080/midway
for mock-server admin-ui.
npm run start-mock
Starting mock server on HTTPS port -
To enable https
, add httpsPort
with the desired port number in server start script as shown below:
midway.start({
port: 8080,
httpsPort: 4444,
host: 'localhost',
mockedDirectory: './test/resources/mocked-data',
project: 'HelloMidway'
});
method
value in the midway.route()
object to any one of the following desired values:
Returning different data set for the same mocked route (Variants) -
Variants allows to return a different set of data for the same mocked route. To add one or more variants, attach the variant object to midway.route()
as shown below:
midway.route({
id: 'message',
label: 'Hello message',
path: '/message',
method: 'GET',
variantLabel: 'hello world',
handler: function (req, reply) {
reply({message:'Hello World'})
}
})
.variant({
id: 'universe',
label: 'hello universe',
handler: function (req, reply) {
reply({message:'hello universe'})
}
})
.variant({
id: 'universe',
label: 'hello galaxy',
handler: function (req, reply) {
reply({message:'Hello Galaxy'})
}
});
To get a different set of response, go to admin-ui and select a different variant for the above route and hit http://localhost:8080/message
on your favorite browser.
Storing mocked response in a file - This feature allows you to respond with a static data stored in a file instead of hard coding the response data in the routes definition.
// Automatic reply of the file
midway.route({
id: 'Get Collection',
label: 'Get Collections',
path: '/product/grouping/api/collection/test',
method: 'GET',
variantLabel: 'test-1',
handler: function(req, reply) {
midway.util.respondWithFile(this, reply);
}
})
.variant({
id: 'universe',
label: 'test-2',
handler: function (req, reply) {
midway.util.respondWithFile(this, reply);
}
});
In the above setup, file needed for default route handler (test-1
) should be located at (file location/name is based on mockedDirectory/route/method/[default|variant_name].{ext}
)
./mocks/product/grouping/api/collection/GET/default.{ext}
If this would be a POST call than the file should have been at
./mocks/product/grouping/api/collection/POST/default.{ext}
The file name for variants should change from default.{ext}
to universe.{ext} in above example that is the file name should be the variant name.
To mock live services, your application should allow to configure it to be directed to a mock service instead of live services as shown below:
Please update your app server or application by changing the host name of your live service with the host name for your mock server.
endpoints.js
file.